#include "../include/Spl_list.h"

Double_Link_List* init_double_link_list()
{
    Double_Link_List* L = (Double_Link_List*)malloc(sizeof(Double_Link_List));
    if (L == NULL) {
        ERRLOG("创建链表失败\n");
        return NULL;
    }
    else
    L->head = NULL;
    return L;
}
//插入
void insert_double_node(Double_Link_List* list, Elenemt_data data, int index)
{
    if (list == NULL) {
        ERRLOG("插入节点前请先创建链表\n");
        return;
    }
    if (index < 0) {
        ERRLOG("插入位置不合法\n");
        return;
    }
    Double_Node* newNode = (Double_Node*)malloc(sizeof(Double_Node));
    if (newNode == NULL) {
        ERRLOG("创建新节点失败\n");
        return;
    }
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    if (index == 1)
    {
        newNode->next = list->head;
        if (list->head!= NULL)
            list->head->prev = newNode;
        list->head = newNode;
        return;
    }

Double_Node * P =list->head;
for (size_t i = 0; i < index-1 && P != NULL; i++)
{
    P = P->next;
}
if (P == NULL)
{
    ERRLOG("插入位置超出链表范围\n");
    free(newNode);
    return;
}
  newNode->next = P->next;
  if (P->next != NULL)
  {
    P->next->prev = newNode;
  }
  newNode->prev = P;
  P->next = newNode;
  return;
}

//遍历
void print_forward_double_link_list(Double_Link_List* list)
{
    if (list == NULL || list->head == NULL) 
    {
        ERRLOG("链表为空\n");
        return;
    }
    Double_Node* p = list->head;
    while (p!= NULL) 
    {
        printf("账号:%s\t密码:%s\tid:%d\n", p->data.name, p->data.password, p->data.id);
        p = p->next;
    }
    printf("\n");
    return;
}
//删除
Elenemt_data delete_double_node(Double_Link_List* list, int index)
{
    Elenemt_data delete_data;
    if (list == NULL)
    {
        ERRLOG("删除节点前请先创建链表\n");
        return delete_data;
    }
    if (list->head == NULL)
    {
        ERRLOG("链表为空，无法删除\n");
        return delete_data;
    }
    if (index == 1)
    {
        Double_Node* temp = list->head;
        list->head = temp->next;
        temp->next->prev = NULL;
        delete_data = temp->data;
        return delete_data;
    }
    
    Double_Node* p1 = list->head;
    Double_Node* p2 = p1->next;

    for (size_t i = 0; i < index-1 && p1->next != NULL; i++)
    {
        p1 = p1->next;
        p2 = p1->next;
    }
    if (p1->next == NULL)
    {
        ERRLOG("删除越界访问\n");
        return delete_data;
    }
    p1->next = p2->next;
    if (p2->next != NULL)
    {
        p2->next->prev = p1;
    }
    delete_data = p2->data;
    free(p2);
    return delete_data;
}

//查找函数
Double_Node* find_double_node_name(Double_Link_List* list, char* name)
{
    if (list == NULL || list->head == NULL) 
    {
        ERRLOG("链表为空\n");
        return NULL;
    }
    Double_Node* p = list->head;
    while (p!= NULL) 
    {
        if (!strcmp(p->data.name, name))
            return p;
        p = p->next;
    }
    return NULL;
}

void clear_double_link_list(Double_Link_List* list)
{
    if (list == NULL || list->head == NULL) 
    {
        ERRLOG("链表为空，无需清理\n");
        return;
    }
    Double_Node* p = list->head;
    Double_Node* temp = p->next;
    while (p!= NULL)
    {
        free(p);
        p = temp;
        if (temp != NULL)
        {
            temp = temp->next;
        }
        
    }
    return;
}

void free_double_link_list(Double_Link_List** list)
{
    if (list!= NULL)
    {
        if (NULL == *list)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == (*list)->head)
    {
        ERRLOG("链表内无数据");
        free(*list);
        *list = NULL;
        return;
    }

        clear_double_link_list(*list);
        free(*list);
        *list = NULL;
    }
    return;
}

// 写入到文件中
void F_print(Double_Link_List* list , const char * file_name)
{
    FILE * fp = fopen( file_name , "w");
    if (NULL ==fp)
    {
        ERRLOG("文件创建失败");
        return ;
    }
    // 光标移动到开头
    fseek(fp , 0 , SEEK_SET);

    Double_Node * ptr = list->head;
    // 遍历写入
    for (size_t i = 0; NULL != ptr ; i++)
    {
        fprintf( fp , "%s\t%s\t%d\n" ,ptr->data.name , ptr->data.password , ptr->data.id);
        ptr = ptr->next;
    }
    
    fclose(fp);
}
// 从文件中读取
void F_scanf(Double_Link_List* list , const char * file_name)
{
    // 以只读打开
    FILE*  fp = fopen( file_name , "r");
    if (NULL ==fp)
    {
        ERRLOG("文件创建失败");
        return ;
    }
    // 移动光标到头
    fseek( fp , 0 , SEEK_SET );

    // 判断文件行数
    char ch = '0';  // 接收文件的字符
    int len = 0;      // 判断文件的行数
    while (EOF != (ch = fgetc(fp)))
    {
        if ('\n'  == ch)
        {
            len++;
        }
    }

    // 移动光标到头
    fseek( fp , 0 , SEEK_SET );
    Elenemt_data data;
    Double_Node * ptr = list->head;
    for (size_t i = 0; i < len; i++)
    {
        fscanf( fp , "%s\t%s\t%c\n" , data.name , data.password , &data.id);
        insert_double_node( list , data , i + 1 );
    }

    fclose(fp);
    return ;
}